package com.mangguo.tingjian.content.controller;


import com.mangguo.tingjian.common.Result;
import com.mangguo.tingjian.content.controller.rsp.ContentSimpleRsp;
import com.mangguo.tingjian.content.mapper.DwAudioMapper;
import com.mangguo.tingjian.content.mapper.DwPlayerMapper;
import com.mangguo.tingjian.content.service.IDwAudioService;
import com.mangguo.tingjian.content.service.IDwPlayerService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.*;

/**
 * <p>
 * 音频同步表 前端控制器
 * </p>
 *
 * @author auto
 * @since 2019-11-24
 */
@RestController
@RequestMapping("/content/dw-audio")
@AllArgsConstructor
@Slf4j
public class DwAudioController {

    private final IDwAudioService dwAudioService;

    private final IDwPlayerService dwPlayerService;

    private final DwAudioMapper dwAudioMapper;

    private final DwPlayerMapper dwPlayerMapper;


    private final ExecutorService executorService = new ThreadPoolExecutor(3, 6, 30L,TimeUnit.SECONDS, new LinkedBlockingQueue<>());;

    /**
     * 简介标签数据
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/simple/{start}/{end}")
    public Result<ContentSimpleRsp> simpleTag(@PathVariable("start") Integer start, @PathVariable("end") Integer end){
        Future<ContentSimpleRsp> contentSimpleRspFuture = executorService.submit(() -> dwAudioMapper.getContentSimpleRspBetweenDate(start, end));
        Future<ContentSimpleRsp> contentSimpleRspAllFuture = executorService.submit(dwAudioMapper::getContentSimpleRspAll);
        Future<ContentSimpleRsp> contentSimpleRspPlayFuture = executorService.submit(() -> dwPlayerMapper.getContentSimpleRspBetweenDate(start, end));
        ContentSimpleRsp contentSimpleRsp = new ContentSimpleRsp();
        try {
            contentSimpleRsp.setAudioNew(contentSimpleRspFuture.get().getAudioNew() != null ? contentSimpleRspFuture.get().getAudioNew(): 0L);
            contentSimpleRsp.setAudioAll(contentSimpleRspAllFuture.get().getAudioAll() != null ? contentSimpleRspAllFuture.get().getAudioAll(): 0L);
            contentSimpleRsp.setDurationNew(contentSimpleRspFuture.get().getDurationNew() != null ? contentSimpleRspFuture.get().getDurationNew(): 0L);
            contentSimpleRsp.setDurationAll(contentSimpleRspAllFuture.get().getDurationAll() != null ? contentSimpleRspAllFuture.get().getDurationAll(): 0L);
            contentSimpleRsp.setAlbumNew(contentSimpleRspFuture.get().getAlbumNew() != null ? contentSimpleRspFuture.get().getAlbumNew(): 0L);
            contentSimpleRsp.setAlbumAll(contentSimpleRspAllFuture.get().getAlbumAll() != null ? contentSimpleRspAllFuture.get().getAlbumAll(): 0L);
            contentSimpleRsp.setPlayCount(contentSimpleRspPlayFuture.get().getPlayCount() != null ? contentSimpleRspPlayFuture.get().getPlayCount(): 0L);
            contentSimpleRsp.setPlayCountAvg(contentSimpleRspPlayFuture.get().getPlayCountAvg() != null ? contentSimpleRspPlayFuture.get().getPlayCountAvg(): 0L);
            contentSimpleRsp.setPlayTime(contentSimpleRspPlayFuture.get().getPlayTime()!= null ? contentSimpleRspPlayFuture.get().getPlayTime(): 0L);
            contentSimpleRsp.setPlayTimeAvg(contentSimpleRspPlayFuture.get().getPlayTimeAvg() != null ? contentSimpleRspPlayFuture.get().getPlayTimeAvg(): 0L);
        }catch (Exception e){
            log.error("获取数据异常", e);
        }



//        contentSimpleRsp.setAudioNew(dwAudioService.countAudioBetweenDate(start, end));
//        contentSimpleRsp.setAudioAll(dwAudioService.countAudioAll());
//        contentSimpleRsp.setDurationNew(dwAudioService.sumAudioDurationBetweenDate(start, end));
//        contentSimpleRsp.setDurationAll(dwAudioService.sumAudioDurationAll());
//        contentSimpleRsp.setAlbumNew(dwAudioService.countAlbumBetweenDate(start, end));
//        contentSimpleRsp.setAlbumAll(dwAudioService.countAlbumAll());
//        contentSimpleRsp.setPlayCount(dwPlayerService.countPlayBetweenDate(start, end));
//        contentSimpleRsp.setPlayCountAvg(dwPlayerService.countPlayAvgBetweenDate(start, end));
//        contentSimpleRsp.setPlayTime(dwPlayerService.sumPlayTimeBetweenDate(start, end));
//        contentSimpleRsp.setPlayTimeAvg(dwPlayerService.sumPlayAvgTimeBetweenDate(start, end));
        return Result.success(contentSimpleRsp);
    }


    /**
     * 模糊查询5条音频名字
     * @param name
     * @return
     */
    @GetMapping("/query/audio")
    public Result<List<String>> queryAudioNameLike(@RequestParam("name") String name) {
        return Result.success(dwAudioService.queryAudioNameLike(name));
    }

    /**
     * 模糊查询5条专辑名字
     * @param name
     * @return
     */
    @GetMapping("/query/album")
    public Result<List<String>> queryAlbumNameLike(@RequestParam("name") String name) {
        return Result.success(dwAudioService.queryAlbumNameLike(name));
    }

    /**
     * 模糊查询5条主播
     * @param name
     * @return
     */
    @GetMapping("/query/author")
    public Result<List<String>> queryAuthorNameLike(@RequestParam("name") String name) {
        return Result.success(dwAudioService.queryAuthorNameLike(name));
    }

    /**
     * 模糊查询5个生产机构
     * @param name
     * @return
     */
    @GetMapping("/query/cmo")
    public Result<List<String>> queryCmoNameLike(@RequestParam("name") String name) {
        return Result.success(dwAudioService.queryCmoNameLike(name));
    }

}
